package com.duang.framework.config;

import java.util.ArrayList;


import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;

import com.duang.framework.cache.CacheConfiguration;
import com.duang.framework.cache.PageCachingFilter;

public abstract class AbstractConfiguration extends
		ApplicationConfigurationPluginSupport {

	protected Map<String, String> mobileUrlMap;
	public static final String ERROR_VIEW = "error";
	protected Map<CacheConfiguration, List<String>> pageCacheMap = new HashMap<CacheConfiguration, List<String>>();

	public static enum MyEnum {

	}

	final void configure() {
		configurePlugins();
		configurePageCachings();
		if (usePageCache() && pageCacheMap.size() > 0) {
			// generate filter
			for (Entry<CacheConfiguration, List<String>> entry : pageCacheMap
					.entrySet()) {
				CacheConfiguration cacheConfiguration = entry.getKey();
				String name;
				if ((name = cacheConfiguration.getName()) == null
						|| name.equals("")) {
					cacheConfiguration.setName(entry.getValue().get(0));
				}
				this.cacheProvider.createCache(cacheConfiguration);
				PageCachingFilter pageCachingFilter = this.cacheProviderFactory
						.createPageCachingFilter(null);
				if (pageCachingFilter == null) {
					throw new ConfigureApplicationException(
							"if you want to use Page Cache,must configure cache provider which is ehcache");
				}
				Dynamic dynamic = SystemConfiguration.getServletContext()
						.addFilter(cacheConfiguration.getName(),
								pageCachingFilter);
				dynamic.setInitParameter("cacheName",
						cacheConfiguration.getName());
				EnumSet<DispatcherType> numSet = EnumSet
						.noneOf(DispatcherType.class);
				numSet.add(DispatcherType.REQUEST);
				numSet.add(DispatcherType.INCLUDE);
				numSet.add(DispatcherType.FORWARD);
				String[] url = entry.getValue().toArray(new String[0]);
				dynamic.addMappingForUrlPatterns(numSet, true, url);
			}
		}

		configureServices();
	}

	protected final void configurePageCaching(
			CacheConfiguration cacheConfiguration, List<String> urlPatternList) {

		if (urlPatternList == null || urlPatternList.size() == 0) {
			return;
		}

		List<String> urlList;
		if ((urlList = pageCacheMap.get(cacheConfiguration)) == null) {
			pageCacheMap.put(cacheConfiguration, urlPatternList);
		} else {
			urlList.addAll(urlPatternList);
		}
	}

	protected final void configurePageCaching(String url,
			CacheConfiguration cacheConfiguration) {
		List<String> urlList;
		if ((urlList = pageCacheMap.get(cacheConfiguration)) == null) {
			urlList = new ArrayList<String>();
			pageCacheMap.put(cacheConfiguration, urlList);
		}
		urlList.add(url);
	}

	protected void configurePageCachings() {

	}

	public Map<String, String> getMobileUrlMap() {
		if (mobileUrlMap == null) {
			mobileUrlMap = configMobileViewPath();
		}
		return mobileUrlMap;
	}

	public abstract Map<String, String> configMobileViewPath();

	protected void start() {

	}

	protected void end() {
	}

}
